/**
 * 2016年9月9日
 */
package algorithm.nowcoder.string;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Deque;
import java.util.LinkedList;

import java.io.File;

import algorithm.CIG.base.TreeBase;
import algorithm.CIG.base.TreeNode;


/**
 * @author Alex
 *
 */


public class Lesson1_2 {

	/**
	 * 有一棵二叉树，请设计一个算法，按照层次打印这棵二叉树。
	 * 给定二叉树的根结点root，请返回打印结果，结果按照每一层一个数组进行储存，
	 * 所有数组的顺序按照层数从上往下，且每一层的数组内元素按照从左往右排列。
	 * 保证结点数小于等于500。
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		
		double k = Math.ceil(-0.5);
	System.out.println(k);
		TreeNode tn = get();
		System.out.println(getH(tn,0));
		f(tn);

	}
	static int res [][] = new int [100][100];
	public static int getH(TreeNode root,int level){
		if(root == null){
			return level;
		}else{
			int lH = getH(root.left,level+1);
			int rH = getH(root.right,level+1);
			return lH>rH?lH:rH;
		}
		
	}
	public static void f(TreeNode root){
		if(root!=null){
			
			Deque<TreeNode> de = new LinkedList<>();
			de.addLast(root);
			int level = 0;
			int seq = 0;
		
			TreeNode last = root;
			TreeNode nLast = null;
			boolean re = false;
			while(!de.isEmpty()){
				TreeNode t = de.pollFirst();
				System.out.print(t.val);
				res[level][seq] = t.val;
				seq ++ ;
				if(t.left!=null){
					de.addLast(t.left);
					nLast = t.left;
				}
				if(t.right!=null){
					de.addLast(t.right);
					nLast = t.right;
				}
				
				if(t == last){
					last = nLast;
					level ++ ;
					seq = 0;
					System.out.println();
				}
				
			}
			System.out.println();
		}
	}
	public static TreeNode get(){
		return TreeBase.generateTree();
	}

}
